/* 
 * Created on 2006-1-5
 * Last modified on 2009-12-1
 * www.YeQiangWei.com
 */
package com.yeqiangwei.commons.util;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.List;

import org.apache.log4j.Logger;
import org.jdom.CDATA;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.input.SAXBuilder;
import org.jdom.output.Format;
import org.jdom.output.XMLOutputter;


public class XmlOperate {
	
	private static final Logger logger = Logger.getLogger(XmlOperate.class.getName());
	
	private Document document = null;
	
	private String xmlPath = "";

	protected XmlOperate(){
		
	}

	public XmlOperate(String xmlpath){
		try{
			this.document = (new SAXBuilder().build(this.xmlPath = xmlpath));
		}catch(Exception e){
			logger.error(e.toString());
		}
	}
	
	public XmlOperate(InputStream stream){
		try{
			this.document = (new SAXBuilder().build(stream));
		}catch(Exception e){
			logger.error(e.toString());
		}
	}
	
	public XmlOperate(URL url){
		try{
			this.document = (new SAXBuilder().build(url));
		}catch(Exception e){
			logger.error(e.toString());
		}
	}
	
	@SuppressWarnings("unchecked")
	public List<Element> getRootElementChlildren(){
		return document.getRootElement().getChildren();
	}
	
	public Element getRootElement(String child){
		try{
			return this.document.getRootElement();
		}catch(Exception e){
			logger.error(e.toString());
		}
		return null;
	}
	
	/**
	 * @deprecated
	 * @param child
	 * @return
	 */
	public Element getElement(String child){
		try{
			return (Element)this.document.getRootElement().getChild(child);
		}catch(Exception e){
			logger.error(e.toString());
		}
		return null;
	}
	
	public Element getRootElement(){
		try{
			return (Element)this.document.getRootElement();
		}catch(Exception e){
			logger.error(e.toString());
		}
		return null;
	}
	
	public Element getChild(Element ele, String childName) {
		if (ele != null) {
			try {
				return ele.getChild(childName);
			} catch (Exception e) {
				logger.error(e.toString());
			}
		}
		return null;
	}
	
	public static final String getValueTrim(Element ele){
		String value = ele.getValue();
		if(value != null){
			value = value.trim();
		}
		return value;
	}
	
	public String getText(Element ele, String propertyName) {
		if (ele != null) {
			try {
				return ele.getText();
			} catch (Exception e) {
				logger.error(e.toString());
			}
		}
		return null;
	}
	
	public String getChildText(Element ele, String c){
		if(ele!=null){
			try{
				return ele.getChildText(c);
			}catch(Exception e){
				logger.error(e.toString());
			}
		}
		return null;
	}
	

	public void setText(Element ele, String text) {
		if (ele == null) {
			return;
		}
		ele.setText(text);		
		org.jdom.output.XMLOutputter xml = new org.jdom.output.XMLOutputter();
		try {
			Format format = Format.getPrettyFormat(); // 格式化文档
			xml.setFormat(format);
			xml.output(this.document, new FileOutputStream(xmlPath));
		} catch (FileNotFoundException e) {
			logger.error(e.toString());
		} catch (IOException e) {
			logger.error(e.toString());
		}
	}
	
	public void setChildText(Element parentEle, String childName, String text) {
		if (parentEle == null) {
			return;
		}
		Element childEle = parentEle.getChild(childName);
		if (childEle == null) {
			childEle = this.addChild(parentEle, childName);
		}
		childEle.setText(text);
		org.jdom.output.XMLOutputter xml = new org.jdom.output.XMLOutputter();
		try {
			Format format = Format.getPrettyFormat(); // 格式化文档
			xml.setFormat(format);
			xml.output(this.document, new FileOutputStream(xmlPath));
		} catch (FileNotFoundException e) {
			logger.error(e.toString());
		} catch (IOException e) {
			logger.error(e.toString());
		}
	}
	
	public void setCDATA(Element ele, String child, String str) {
		CDATA cdata = new CDATA(str);
		Element eleU = ele.getChild(child);
		if (eleU == null) {
			eleU = this.addChild(ele, child);
		}
		eleU.setContent(cdata);
		org.jdom.output.XMLOutputter xml = new org.jdom.output.XMLOutputter();
		try {
			Format format = Format.getPrettyFormat(); // 格式化文档
			xml.setFormat(format);
			xml.output(this.document, new FileOutputStream(xmlPath));
		} catch (FileNotFoundException e) {
			logger.error(e.toString());
		} catch (IOException e) {
			logger.error(e.toString());
		}
	}
	
	public String getCDATA(Element ele, String child){
		return getChildText(ele,child);
	}
	
	
	public Element setRootElement(String name) {
		Element rootElement = new Element(name);
		this.document.setRootElement(rootElement);
		this.document.setRootElement(rootElement);
		org.jdom.output.XMLOutputter xml = new org.jdom.output.XMLOutputter();
		try {
			Format format = Format.getPrettyFormat(); // 格式化文档
			xml.setFormat(format);
			xml.output(this.document, new FileOutputStream(xmlPath));
		} catch (FileNotFoundException e) {
			logger.error(e.toString());
		} catch (IOException e) {
			logger.error(e.toString());
		}
		return rootElement;
	}
	
	/**
	 * 
	 * @param ele
	 * @param childName
	 * @return child element
	 */
	public Element addChild(Element parent, String childName) {
		Element child = new Element(childName);
		parent.addContent(child);
		try {
			Format format = Format.getPrettyFormat(); // 格式化文档
			XMLOutputter outputter = new XMLOutputter(format);
			outputter.output(this.document, new FileOutputStream(xmlPath));
		} catch (FileNotFoundException e) {
			logger.error(e.toString());
		} catch (IOException e) {
			logger.error(e.toString());
		}
		return child;
	}
	
	public String getXmlPath() {
		return xmlPath;
	}

	public void setXmlPath(String xmlPath) {
		this.xmlPath = xmlPath;
	}
}
